﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using WorkFlowSystem.DataBase;
using WorkFlowSystem.Common;
using System.Data.SqlClient;

namespace WorkFlowSystem.Business
{
    class WorkReviewDv
    {
        //一览画面DATA取得
        public int getWorkData(bool isInitFlag, string uid, string year, string month, string status, ref DataTable dt, ref int dataCnt)
        {
            if (isInitFlag)
            {
                //初始化时显示15条空记录
                for (int i = 0; i < 15; i++)
                {
                    dt.Rows.Add(dt.NewRow());
                }
            }
            else
            {
                if (month.Length == 1)
                {
                    month = "0" + month;
                }

                int yearMonth = Int32.Parse(year + month);

                DBOperator dbo = new DBOperator();

                dbo.ConnectDB();
                //update_uid 取更新者姓名
                string sql = " SELECT s.s_name, w.w_start_date, w.w_end_date , " +
                             " case when w.w_date_name = 'D' then " +
                             " CONVERT(varchar(10), w.w_start_date, 120)" +
                             " else w.w_date_name end w_date_name , " +
                             " w.w_status , u.u_name , w.w_update_time, w.w_uid, s.sid, " +
                             " (select u_name from user_info where w.w_update_uid = uid) as w_update_uname "+ //取更新者用户名
                             " FROM  work_info w , user_info u,standard s " +
                             " WHERE w.w_sid = s.sid and " +
                             " s.s_tab_no = 1 and " +
                             " w.w_uid in (select uid from user_info where u_did = @user_did) and " +
                             " w.w_uid = u.uid and " +  //取员工用户名
                             " w.w_status = @status and " +
                             " @yearMonth between year(w_start_date)*100+month(w_start_date) and  year(w_end_date)*100+month(w_end_date)";


                List<SqlParameter> para_list = new List<SqlParameter>();
                
                para_list.Add(new SqlParameter("@status", status));
                para_list.Add(new SqlParameter("@uid", uid));
                para_list.Add(new SqlParameter("@yearMonth", yearMonth));
                para_list.Add(new SqlParameter("@user_did", CurrentUser.user_did));

                dbo.ExecuteDataTable(sql, para_list, false, ref dt);

                dbo.CloseDB();

                dataCnt = dt.Rows.Count;

                int dtRowCount = dt.Rows.Count;


                //查询数据如果不满15条，补充数据到15条。
                if (dtRowCount < 15)
                {
                    for (int i = 0; i < 15 - dtRowCount; i++)
                    {
                        dt.Rows.Add(dt.NewRow());
                    }
                }

            }

            return 0;

        }

    }
}
